package com.wjh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjh.constants.SystemConstants;
import com.wjh.mapper.TagMapper;
import com.wjh.pojo.Study;
import com.wjh.pojo.Tag;
import com.wjh.service.TagService;
import com.wjh.utils.BeanCopyUtils;
import com.wjh.utils.ResponseResult;
import com.wjh.vo.StudyVo;
import com.wjh.vo.TagVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 标签表(Tag)表服务实现类
 *
 * @author makejava
 * @since 2023-03-15 16:38:54
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Override
    public Tag queryById(Integer id) {
        //通过ID查询对应的标签
        LambdaQueryWrapper<Tag> queryWrapper =
                new LambdaQueryWrapper<>();
        queryWrapper.eq(Tag::getId,id);
        //判断标签是否可用
        queryWrapper.eq(Tag::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        //查询
        Tag tag = getOne(queryWrapper);
        //返回
        return tag;
    }

    @Override
    public ResponseResult queryStudyAll(String name) {
        LambdaQueryWrapper<Tag> queryWrapper =
                new LambdaQueryWrapper<>();
        //判断是否传递name参数
        queryWrapper.like(StringUtils.hasText(name), Tag::getName,name);
        //判断状态是否正常
        queryWrapper.eq(Tag::getStatus,SystemConstants.ARTICLE_STATUS_NORMAL);
        //查询
        List<Tag> list = list(queryWrapper);
        //封装成Vo对象返回
        List<TagVo> tagVos = BeanCopyUtils.copyBeanList(list, TagVo.class);
        //存入redis，
        return ResponseResult.okResult(tagVos);
    }
}
